<template>
  <div class="search">
    <van-nav-bar
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    >
      <template #right>
        <van-search
          v-model="value"
          placeholder="请输入搜索关键词"
          input-align="left"
          @input="getValue"
        />
      </template>
    </van-nav-bar>
    <div class="box">
      <div class="palcehode"></div>
      <div class="collectBox">
        <div class="collectItem">
          <div class="item" v-for="(item, index) in collectArr" :key="index">
              <div class="icon" v-if="hotFlag">
                  <img src="../assets/images/ishot.png" class="auto-img">
              </div>
            <div class="img" @click="detail(item)">
              <img :src="item.smallImg" class="auto-img" />
            </div>
            <div class="zn-name">{{ item.name }}</div>
            <div class="en-name">{{ item.enname }}</div>
            <div class="priceAndIcon">
              <div class="price">￥{{ item.price }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "search",
  data() {
    return {
      value: "",
      collectArr: [],
      hotSearch:[],
      hotFlag:true
    };
  },
  methods: {
    //   设置初始化热门商品
    getHot(){
    this.axios({
          method: "get",
          url: "/search",
          params: {
            name:"",
          },
        })
          .then((res) => {
            if (res.data.code == "Q001"){
                res.data.result.forEach(item => {
                     if(item.isHot==1){
                         this.collectArr.push(item)
                     }
                });
            }
          })
          .catch((err) => {
            console.log(err);
          });
    },
    getValue() {
      if (this.value != "") {
        this.axios({
          method: "get",
          url: "/search",
          params: {
            name:this.value,
          },
        })
          .then((res) => {
            if (res.data.code == "Q001"){
                this.hotFlag=false;
                 this.collectArr = res.data.result;
                 console.log(this.collectArr);
            }
          })
          .catch((err) => {
            console.log(err);
          });
      }else{
          this.collectArr=[];
           this.hotFlag=true;
          this.getHot()
      }
    },
    detail(data){
          this.$router.push({name: 'Detail', params: {pid:data.pid}});
    },
    onClickLeft() {
      this.$router.go(-1);
    },
  },
  created(){
      this.getHot()
  }
};
</script>
<style lang="less" scoped>
@import url("../assets/base.less");
.box {
  position: absolute;
  top: 46px;
  bottom: 0px;
  width: 100%;
  overflow-y: auto;
  .palcehode {
    height: 100px;
    width: 100%;
    background-color: @mainColor;
  }
  .collectBox {
    padding: 0px 10px;
    transform: translateY(-20px);
    .collectItem {
      border-radius: 10px 10px 0px 0px;
      height: 195px;
      width: 355px;
      background-color: #fff;
      display: flex;
      flex-wrap: wrap;
      .item {
        flex: 0 0 33.33%;
        padding: 10px 8px;
        box-sizing: border-box;
        .icon{
            height: 20px;
            width: 20px;
            position: absolute;
        }
        .img {
          height: 100px;
          width: 100%;
        }
        .zn-name {
          font-size: 12.5px;
          margin-top: 10px;
        }
        .en-name {
          margin-top: 5px;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 1;
          -webkit-box-orient: vertical;
        }
        .priceAndIcon {
          margin-top: 10px;
          display: flex;
          .price {
            font-size: 12.5px;
            font-weight: bold;
            color: @mainColor;
          }
          .icon {
            height: 20px;
            width: 20px;
            margin-left: auto;
          }
        }
      }
    }
  }
}
/deep/ .van-nav-bar__right {
  left: 60px;
}
/deep/ .van-search {
  background-color: transparent;
  border-radius: 20px;
}
/deep/ .van-nav-bar .van-icon {
  color: @mainColor;
}

/deep/ .van-nav-bar__text {
  color: @mainColor;
}
</style>